package com.wn.controller;

import com.wn.bean.R;
import com.wn.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yellow Docter
 * @date 2022 -05-16
 * @desc
 */
@RestController
@Slf4j
public class LoginController {

    /*
     * 修改登录操作:
     *
     * 01 修改数据库的 密码 和 盐值
     *
     * 02 修改 登录操作
     *      1.修改自定的realm
     *      2.配置加密器 shiroConfig 进行配置
     *
     *
     *
     * @param user
     * @return
     */

    @PostMapping("/login")
    public R login(@RequestBody User user){

        try {
            log.info("前端提交的user:{}",user);
            // 登录流程
            // 01 拿到subject
            Subject subject = SecurityUtils.getSubject();

            //02 创建token
            UsernamePasswordToken token = new UsernamePasswordToken(
                    user.getUsername(),
                    user.getPassword()
            );

            //03 subject.login()
            subject.login(token);
            return R.OK("登录成功");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ERROR("登录失败");
    }

}
